package com.example.cloud.controller.system;

import com.alibaba.fastjson.JSONArray;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.system.Organ;
import com.example.cloud.model.system.User;
import com.example.cloud.service.system.OrganService;
import com.example.cloud.service.system.UserService;
import com.example.cloud.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangtao on 2018/1/26.
 */
@RestController
@RequestMapping("/organ")
public class OrganController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(OrganController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private OrganService organService;

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        return ResultUtil.success(organService.findOneFromCache(id));
    }

    /**
     * 组织机构查询
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        JSONArray jsonArray = new JSONArray();
        List<Organ> organList = organService.findAllFromCache();
        if (!organList.isEmpty()) {
            jsonArray = organService.createOrganTree(organList);
        }
        return ResultUtil.success(jsonArray);
    }

    /**
     * 组织机构权限查询
     * @param userId
     * @return
     */
    @GetMapping("/findByUser/{userId}")
    public Result findByUser(@PathVariable Long userId) {
        JSONArray jsonArray = new JSONArray();
        User user = userService.findOneFromCache(userId);
        List<Organ> organs = organService.findAllFromCache();
        if (user != null) {
            List<Long> ids = new ArrayList<Long>();
            for (Organ organ : user.getOrgans()) {
                ids.add(organ.getId());
            }
            for (Organ organ : organs) {
                if(ids.contains(organ.getId())){
                    organ.setChecked(true);
                }
            }
            if(!organs.isEmpty()){
                jsonArray = organService.createOrganTree(organs);
            }
        }
        return ResultUtil.success(jsonArray);
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody Organ organ){
        organService.doSave(organ);
    }

    @PostMapping("/delete/{id}")
    public void delete(@PathVariable Long id){
        organService.doDelete(id);
    }

    @PostMapping("/addOrgan/{userId}")
    public void addOrgan(@PathVariable Long userId, @RequestBody Organ organ){
        User user = userService.findOneFromCache(userId);
        user.setOrgans(organService.getOrganList(user, organ));
        userService.doSave(user);
    }
}
